package com.example.ah_geen_pda;

import android.content.Intent;
import android.os.Bundle;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.ah_geen_pda.base.Constant;
import com.example.ah_geen_pda.base.DataBindBaseViewHolder;
import com.example.ah_geen_pda.databinding.ItemMainBinding;
import com.example.ah_geen_pda.utils.MyUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_TabIndexGridEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

public class MainActivity extends AppCompatActivity {

    private List<FreeUI_TabIndexGridEntity> mfgItems;
    private MainAdapter mAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RecyclerView rvMFGList = findViewById(R.id.rvMFGList);
        mfgItems = new ArrayList<>();
        mAdapter = new MainAdapter(null);
        rvMFGList.setAdapter(mAdapter);
        initAssets();
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            Intent intent = new Intent();
            FreeUI_TabIndexGridEntity entity = mAdapter.getData().get(position);
            try {
                Class<?> clazz = Class.forName(entity.getSkipClass());
                intent.putExtra("showPlugin", entity.getShowPlugin());
                intent.setClass(MainActivity.this, clazz);
                startActivity(intent);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

        });

    }

    private void initAssets() {
        try {
            InputStream mfgJsonStream = getResources().getAssets().open("mfg.txt");
            String mfgJson = FreeApi_StringUtils.getString(mfgJsonStream);
            List<FreeUI_TabIndexGridEntity> mfgEntity = filteringData(new Gson().fromJson(mfgJson,
                    new TypeToken<List<FreeUI_TabIndexGridEntity>>() {
                    }.getType()));
            mAdapter.setNewData(MyUtils.INSTANCE.setPicToDrawable(this,mfgEntity));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    private List<FreeUI_TabIndexGridEntity> filteringData(List<FreeUI_TabIndexGridEntity> entity){
        Iterator<FreeUI_TabIndexGridEntity> it = entity.iterator();
        while (it.hasNext()) {
            FreeUI_TabIndexGridEntity next = it.next();

            try {
                Class<?> aClass = Class.forName(next.getSkipClass());
                String name = aClass.getSimpleName();
//                if (!Constant.AUTHORITYLIST.contains(name)) {
//                    it.remove()
//                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

        }
        return entity;
    }

    private class MainAdapter extends BaseQuickAdapter<FreeUI_TabIndexGridEntity, DataBindBaseViewHolder> {

        public MainAdapter(@Nullable List<FreeUI_TabIndexGridEntity> data) {
            super(R.layout.item_main,data);
        }

        @Override
        protected void convert(DataBindBaseViewHolder helper, FreeUI_TabIndexGridEntity item) {
            ItemMainBinding binding = (ItemMainBinding) helper.getDataBinding();
            binding.setTabIndexGrid(item);
            binding.executePendingBindings();
        }
    }
}